package com.gulimall.guilimallmember.controller;

import java.util.Arrays;
import java.util.Map;

import com.gulimall.common.exception.BizCodeEnum;
import com.gulimall.guilimallmember.exception.PhoneExistException;
import com.gulimall.guilimallmember.exception.UserNameExistException;
import com.gulimall.guilimallmember.feign.CouponFeignService;
import com.gulimall.guilimallmember.vo.MemberLoginVo;
import com.gulimall.guilimallmember.vo.MemberRegisterVo;
import com.gulimall.guilimallmember.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gulimall.guilimallmember.entity.MemberEntity;
import com.gulimall.guilimallmember.service.MemberService;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.R;



/**
 * 会员
 *
 * @author niujiaxing
 * @email sunlightcs@gmail.com
 * @date 2025-05-09 20:18:36
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    CouponFeignService couponFeignService;


    @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("会员昵称张三");
        R membercoupons = couponFeignService.membercoupons();//假设张三去数据库查了后返回了张三的优惠券信息

        //打印会员和优惠券信息
        return R.ok().put("member",memberEntity).put("coupons",membercoupons.get("coupons"));
    }

    /**
     * 用户注册功能
     * @param registerVo
     * @return
     */
    @RequestMapping("/register") // member
    public R register(@RequestBody MemberRegisterVo registerVo) {
        try{
            memberService.register(registerVo);
        }catch (PhoneExistException e){
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        }catch(UserNameExistException e){
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        }

        return R.ok();
    }

    /**
     * 用户登录功能
     * @param vo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){
        MemberEntity member=memberService.login(vo);
        if(member!=null){
            return R.ok().put("data",member);
        }
        else{
            return R.error(BizCodeEnum.LOGINACTT_PASSWORD_ERROR.getCode(), BizCodeEnum.LOGINACTT_PASSWORD_ERROR.getMsg());
        }
    }

    /**
     * 社交登录
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping("/socialLogin")
    public R socialLogin(@RequestBody SocialUser user) throws Exception {
        MemberEntity memberEntity = memberService.socialLogin(user);
        return R.ok().put("data",memberEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("guilimallmember:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("guilimallmember:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("guilimallmember:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("guilimallmember:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("guilimallmember:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
